package top.yongyan0624.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import top.yongyan0624.pojo.Clazz;
import top.yongyan0624.pojo.Dept;
import top.yongyan0624.pojo.Lesson;
import top.yongyan0624.pojo.Student;
import top.yongyan0624.service.ClazzService;
import top.yongyan0624.service.DeptService;
import top.yongyan0624.service.LessonService;
import top.yongyan0624.service.StudentService;

import java.util.List;

/**
 * @author Yongyan
 * @description: TODO
 * @date 2020/12/23
 **/

@Controller
@RequestMapping("/stu")
@SessionAttributes("error")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ClazzService clazzService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private LessonService lessonService;

    @RequestMapping("/toAddStuPage")
    public String toAddStuPage(Model model){
        List<Clazz> clazzList = clazzService.findAllClazz();
        model.addAttribute("clazzList",clazzList);
        List<Dept> deptList = deptService.findAllDept();
        model.addAttribute("deptList",deptList);
        model.addAttribute("error","");

        return "addStuPage";
    }

    @RequestMapping("/addStu")
    public String addStu(Student student,Model model){
        Student s = studentService.findById(student.getSno());
        if (s == null){
            studentService.addStu(student);
        } else {
            model.addAttribute("error","该学号已被录入！");
            List<Clazz> clazzList = clazzService.findAllClazz();
            model.addAttribute("clazzList",clazzList);
            List<Dept> deptList = deptService.findAllDept();
            model.addAttribute("deptList",deptList);

            return "addStuPage";
        }
        return "redirect:/stu/toFindStuAllPage/1/5";
    }

    @RequestMapping("/deleteStu/{id}")
    public String deleteStu(@PathVariable("id") Integer id){
        studentService.deleteStu(id);
        return "redirect:/stu/toFindStuAllPage/1/5";
    }

    @RequestMapping("/toUpdateStuPage/{id}")
    public String toUpdateStuPage(@PathVariable Integer id, Model model){
        Student student = studentService.findById(id);
        model.addAttribute("student",student);
        List<Clazz> clazzList = clazzService.findAllClazz();
        model.addAttribute("clazzList",clazzList);
        List<Dept> deptList = deptService.findAllDept();
        model.addAttribute("deptList",deptList);

        return "updateStuPage";
    }

    @RequestMapping("/updateStu")
    public String updateStu(Student student){
        studentService.updateStu(student);

        return "redirect:/stu/toFindStuAllPage/1/5";
    }


    @RequestMapping("/toFindStuAllPage/{pageNum}/{pageSize}")
    public String toFindAllPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize, Model model){
        PageInfo<Student> pageInfo = studentService.findAllStudent(pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("studentList",pageInfo.getList());
        model.addAttribute("error","");

        return "findAllStuPage";
    }

    @RequestMapping("/findStu")
    public String findStu(Student student,Model model){
        List<Student> studentList = studentService.findStu(student);
        if (studentList.size()<=0){
            model.addAttribute("error","未找到该学生...");

        } else if (student.getSno()==null && student.getName().equals("") && student.getXb().equals("")) {
            model.addAttribute("error","");

        } else {
            model.addAttribute("studentList",studentList);
            model.addAttribute("error","");
            return "findAllStuPage";
        }

        return "redirect:/stu/toFindStuAllPage/1/5";
    }

    @RequestMapping("/toFindSelectPage/{id}")
    public String toFindSelectPage(@PathVariable Integer id,Model model){
        model.addAttribute("id",id);

        List<Lesson> lessonList = lessonService.findAllLesson();
        model.addAttribute("lessonList",lessonList);

        List<Student> studentList = studentService.findSelectById(id);
        Student student = studentList.get(0);
        model.addAttribute("student",student);
        List<Lesson> lessons = student.getLessons();
        model.addAttribute("lessons",lessons);

        List<Integer> selectList =  studentService.findSelectIdById(id);
        model.addAttribute("selectList",selectList);

        return "findSelectPage";
    }

    @RequestMapping("/selectLesson/{id}")
    public String selectLesson(@RequestParam(value = "selectList") List<Integer> selectList, @PathVariable(value = "id") Integer id){
        studentService.selectLesson(selectList,id);
        return "redirect:/stu/toFindStuAllPage/1/5";
    }
}
